import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StorageDemo extends StatefulWidget {
  StorageDemo({Key key}) : super(key: key);

  @override
  _StorageDemoState createState() => _StorageDemoState();
}

class _StorageDemoState extends State<StorageDemo> {


  _saveData() async{
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.setString('username', '林浪痕');
    sp.setInt('age', 31);

  }

  _getData() async{
    SharedPreferences sp = await SharedPreferences.getInstance();
    print(sp.get('username'));
    print(sp.get('age'));
  }

  _removeData() async{
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp.remove('username');
    sp.remove('age');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('本地存储') 
      ), 
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('保存数据'),
              onPressed: _saveData
            ),
            SizedBox(height:10),
            RaisedButton(
              child: Text('获取数据'),
              onPressed: _getData
            ),
            SizedBox(height:10),
            RaisedButton(
              child: Text('清除数据'),
              onPressed: _removeData
            )
          ], 
        ), 
      ),
    );
  }

}